<template>
  <div class="roles">
    <el-button type="primary" class="addrole" @click="dialogVisible = true"
      >添加用户</el-button
    >
    <div class="center">
      <el-table :data="rolelist" border style="width: 100%" table-layout="auto">
        <el-table-column prop="xq" type="expand">
          <template slot-scope="scope">
            <el-row
              :class="['bdbottom', i1 === 0 ? 'bdtop' : '', 'vcenter']"
              v-for="(item1, i1) in scope.row.children"
              :key="item1.id"
            >
              <el-col :span="5">
                <el-tag closable @close="removerightById(scope.row, item1.id)">
                  {{ item1.authName }}
                </el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>

              <el-col :span="19">
                <el-row
                  :class="[i2 == 0 ? '' : 'bdtop', 'vcenter']"
                  v-for="(item2, i2) in item1.children"
                  :key="item2.id"
                >
                  <el-col :span="6">
                    <el-tag
                      type="success"
                      closable
                      @close="removerightById(scope.row, item2.id)"
                      >{{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      :key="item3.id"
                      type="warning"
                      closable
                      @close="removerightById(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="角色id"> </el-table-column>
        <el-table-column prop="roleName" label="角色名称"> </el-table-column>
        <el-table-column prop="roleDesc" label="角色描述"> </el-table-column>
        <el-table-column prop="caozuo" label="操作" width="300px">
          <template slot-scope="scope">
            <el-button
              class="btn3"
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="bianji($event, scope.row, scope.$index)"
              >编辑</el-button
            >

            <el-button
              class="btn3"
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="shanchu($event, scope.row, scope.$index)"
              >删除</el-button
            >

            <el-button
              class="btn3 btn4"
              type="warning"
              icon="el-icon-s-tools"
              @click="showQx(scope.row)"
              size="mini"
              >分配权限</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 添加角色弹出层 -->
    <el-dialog
      title="添加用户"
      :visible.sync="dialogVisible"
      width="30%"
      class="logtitle"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="roleName" label="角色名称">
          <el-input
            type="text"
            class="ipt"
            v-model="ruleForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="roleDesc" label="角色描述">
          <el-input
            type="text"
            class="ipt"
            v-model="ruleForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addRoles">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑橘色弹出层 -->
    <el-dialog
      title="添加用户"
      :visible.sync="kaiguan"
      width="30%"
      class="logtitle"
    >
      <el-form
        :model="bjInfo"
        status-icon
        :rules="rules"
        ref="bjInfo"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="roleName" label="角色名称">
          <el-input
            type="text"
            class="ipt"
            v-model="bjInfo.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item prop="roleDesc" label="角色描述">
          <el-input
            type="text"
            class="ipt"
            v-model="bjInfo.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="kaiguan = false">取 消</el-button>
        <el-button type="primary" @click="bjRoles">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 分配权限 -->
    <el-dialog
      title="设置权限"
      :visible.sync="qx"
      width="50%"
      class="logtitle"
      @close="closedRights"
    >
      <el-tree
        :data="treeData"
        show-checkbox
        node-key="id"
        :props="defaultProps"
        default-expand-all
        :default-checked-keys="defkeys"
        ref="treeRef"
      >
      </el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="qx = false">取 消</el-button>
        <el-button type="primary" @click="qxsure">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
  
  <script>
import {
  getRole,
  AddRole,
  DeleteRole,
  revisRole,
  getRoleList,
  DeleteRoleid,
  reviseQx
} from "../../api";
export default {
  name: "RoleList",
  data() {
    return {
      defaultProps: {
        children: "children",
        label: "authName",
      },
      //默认选择的权限数组
      defkeys: [],
      roleId :'',
      rolelist: [],
      dialogVisible: false,
      kaiguan: false,
      qx: false,
      type: "tree",
      treeData: [],
      ruleForm: {
        roleName: "",
        roleDesc: "",
      },
      bjInfo: {
        roleName: "",
        roleDesc: "",
      },

      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        roleDesc: [
          { required: true, message: "请输入角色描述", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    getRole().then((res) => {
      if (res.data.meta.status == 200) {
        this.rolelist = res.data.data;
      }
    });
  },
  methods: {
    //添加角色
    addRoles() {
      this.dialogVisible = false;
      const { roleName, roleDesc } = this.ruleForm;
      AddRole(roleName, roleDesc).then((res) => {
        if (res.data.meta.status == 201) {
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
          getRole().then((res) => {
            if (res.data.meta.status == 200) {
              this.rolelist = res.data.data;
            }
          });
        } else {
          this.$message({
            message: res.data.meta.msg,
            type: "error",
          });
        }
      });
    },

    //删除
    shanchu(event, row, index) {
      let id = row.id;
      DeleteRole(id).then((res) => {
        if (res.data.meta.status == 200) {
          getRole().then((res) => {
            if (res.data.meta.status == 200) {
              this.rolelist = res.data.data;
            }
          });
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
        }
      });
    },

    //编辑角色
    bianji(event, row, index) {
      this.bjInfo = row;
      this.kaiguan = true;
    },
    bjRoles() {
      this.kaiguan = false;
      const { id, roleName, roleDesc } = this.bjInfo;
      console.log(id, roleName, roleDesc);
      revisRole(id, roleName, roleDesc).then((res) => {
        console.log(res);
        if (res.data.meta.status == 200) {
          getRole().then((res) => {
            if (res.data.meta.status == 200) {
              this.rolelist = res.data.data;
            }
          });
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
        }
      });
    },

    //设置权限
    showQx(role) {
      //获得权限
      this.roleId = role.id
      getRoleList(this.type).then((res) => {
        this.treeData = res.data.data;
      });
      this.getLeafKeys(role, this.defkeys);
      this.qx = true;
    },
    qxsure() {
    const keys = [
        ...this.$refs.treeRef.getCheckedKeys(),
        ...this.$refs.treeRef.getHalfCheckedKeys(),
    ]
    const keysStr = keys.join(',')
    reviseQx(this.roleId,keysStr).then(res=>{
        if (res.data.meta.status == 200) {
          getRole().then((res) => {
            if (res.data.meta.status == 200) {
              this.rolelist = res.data.data;
            }
          });
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
        }
    })
    this.qx = false;
    },
    //获取角色所以权限
    getLeafKeys(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getLeafKeys(item, arr));
    },
    //监听对话框关闭
    closedRights() {
      this.defkeys = [];
    },

    //根据id删除对应权限
    removerightById(role, rightId) {
      DeleteRoleid(role.id, rightId).then((res) => {
        if (res.data.meta.status == 200) {
          role.children = res.data.data;
          this.$message({
            message: res.data.meta.msg,
            type: "success",
          });
        }
      });
    },
  },
};
</script>
  
  <style lang="less" scoped>
.roles {
  .addrole {
    width: 88px;
    height: 32px;
    margin: 5px 0 15px;
  }
  .btn3 {
    height: 20px;
    font-size: 12px;
    padding: 0;
  }
  .btn4 {
    width: 88px;
  }
  /deep/ .el-collapse {
    border: none;
  }
  .el-tag {
    margin: 8px 10px;
  }
  .bdtop {
    border-top: 1px solid #eeee;
  }
  .bdbottom {
    border-bottom: 1px solid #eeee;
  }

  .vcenter {
    display: flex;
    align-items: center;
  }
}
</style>